package com.ptone.messagecenter.common.base;


import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/***
 * 通用工具类
 */
public class Common {

    /**
     * 分隔字符串并转为Long list
     *
     * @param str
     * @param splitStr
     * @return
     */
    public static List<Long> splitStrToLongList(String str, String splitStr) {
        List<String> stringList = Arrays.asList(StringUtils.split(str, splitStr));
        return stringList.stream()
                .filter(Objects::nonNull)
                .map(Long::parseLong)
                .collect(Collectors.toList());
    }

    /**
     * 分隔字符串List并转为Long list
     *
     * @param strList
     * @param splitStr
     * @return
     */
    public static List<Long> splitStrListToLongList(List<String> strList, String splitStr) {
        List<Long> result = new ArrayList<>();
        strList.stream()
                .filter(x -> StringUtils.isNotEmpty(x) && StringUtils.isNotEmpty(splitStr))
                .map(x -> splitStrToLongList(x, splitStr))
                .collect(Collectors.toList())
                .forEach(result::addAll);
        return result;
    }

    /**
     * 合并数组
     *
     * @param a
     * @param b
     * @return
     */
    public static <T> T[] concat(T[] a, T[] b) {
        T[] t = (T[]) Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
        System.arraycopy(a, 0, t, 0, a.length);
        System.arraycopy(b, 0, t, a.length, b.length);
        return t;
    }

    /**
     * 判断是否为空
     *
     * @param obj
     * @return
     */
    public static boolean isBlank(Object obj) {
        return Objects.isNull(obj)
                || (obj instanceof String && StringUtils.isBlank((String) obj))
                || (obj instanceof String[] && ((String[]) obj).length == 0);
    }

    /**
     * 首字母大写
     *
     * @param str
     * @return
     */
    public static String upperCase(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }

}
